#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2011-05-05
@author: shell.xu
'''
import os, sys
import web, pymongo, redis, zmq
import utils
from os import path
# 在路径中增加基础路径，这样可以直接将项目内的所有包导入，而无视路径问题
basedir = path.dirname(path.realpath(__file__))
sys.path.insert(0, basedir)

# 配置体系，整个web的所有资源全部在这里配置
web.config.mongo = pymongo.Connection('localhost', 27017)
web.config.redis = redis.Redis('localhost', 6379, 0)
web.config.db = web.config.mongo['pyblog']
web.config.urls = {'login_redir': '/usr/'}
web.config.bt = utils.beanstalk_conn(parse_yaml = False)
web.config.zmqctx = zmq.Context()
web.config.zmqs = web.config.zmqctx.socket(zmq.REQ)
web.config.zmqs.connect('tcp://127.0.0.1:5556')

# 安装应用
import atc, user
urls = []
urls.extend(user.create_urls('/usr'))
urls.extend(atc.create_urls('/atc'))
app = web.application(urls)

# 配置session系统，mongo和redis两个目标
if web.config.get('sess') is None:
    # mgstore = utils.MongoStore(web.config.mongo, 'sess')
    rdsstore = utils.RedisStore(web.config.redis)
    web.config.sess = web.session.Session(app, rdsstore)

# 三种启动路径，默认参数直接启动，profile参数增加性能测试
# 导入方式是要由uwsgi启动的
if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == 'profile':
        app.run(web.profiler)
    else: app.run()
else: application = app.wsgifunc()
